#!/bin/bash

##################################################
# Simplest wrapper. This wrapper works with the Video Station´s Transcoding Profiles and always will be using MP3 2.0 96kbps or 128kbps when transcoding.
# It derives all parameters to the SynoCommunity´s ffmpeg.

rev="AME_S_1.1"
# Changes:
# AME_S_1.0: Initial release of the simplest wrapper. Fork from AlexPresso. (Deprecated, migrate to AME_S_1.1)
# AME_S_1.1: Correcting the log file.

##################################################
# VARIABLES

WHITE="\u001B[37m"
pid=$$
stderrfile="/tmp/ffmpeg-$pid.stderr"
logfile="/tmp/wrapper_ffmpeg.log"

#########################
# FUNCIONES

function log() {
  echo -e "${WHITE}[$(date '+%Y-%m-%d %H:%M:%S')] [$1] $2" >> $logfile
}
function newline() {
  echo "" >> $logfile
}
function info() {
  log "INFO" "$1"
}

function handle_error() {
  log "ERROR" "Error on line $(caller)"
  endprocess
}

function endprocess() {
  info "========================================[end ffmpeg $pid]"
  newline
  rm -f "$stderrfile"
  exit 1
}
#########################
# EJECUCION

trap endprocess SIGTERM
trap handle_error ERR

newline
info "========================================[start ffmpeg $pid]"
info "DEFAULT_ARGS: $*"

/var/packages/ffmpeg/target/bin/ffmpeg "$@" 2> $stderrfile

endprocess
